package com.logicwin.aibala.action;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipException;

import android.util.Log;

import com.logicwin.aibala.model.androidSWInfo.APKListItem;
import com.logicwin.aibala.model.androidSWInfo.APKTotalInfo;
import com.logicwin.aibala.model.androidSWInfo.AndroidSWInfoService;
import com.logicwin.aibala.services.file.FileConnection;
import com.logicwin.aibala.services.net.NetConnection;
import com.logicwin.aibala.services.xml.XMLParser;
import com.logicwin.aibala.services.zip.ZipUtil;
import com.logicwin.aibala.utils.MyLog;

public class LoadDetailAction implements Runnable{
	private final static String tag = "LoadDetailAction";
	
	private String getDetailUrl = 
			"http://home.kequan001.com/aibala/getSoftwareDetail.php";
	private int mApkId;
	public LoadDetailListener _listener = null;
	
	public boolean startLoad(int apkId, LoadDetailListener listener){
		mApkId = apkId;
		_listener = listener;
		new Thread(this).start();
		return true;
	}

	@Override
	public void run() {
		
		String zipPath =  NetConnection.httpGet(getDetailUrl+"?id="+mApkId, null);
		NetConnection.downLoadFile(zipPath, FileConnection.getCurrentPath()+"/temp.zip");
		
		MyLog.info(tag+"-run", "zip="+zipPath+"  path="+FileConnection.getCurrentPath()+"/temp.zip");
		try {
			ZipUtil.upZipFile(new File(FileConnection.getCurrentPath()+"/temp.zip"), FileConnection.getCurrentPath());
		} catch (ZipException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			String xmlPath = zipPath.substring(zipPath.lastIndexOf("/")+1,zipPath.length()-4)+".xml";
			APKTotalInfo apkTotalInfo = XMLParser.parseDetail(FileConnection.getCurrentPath()+"/"+xmlPath);
			_listener.loadDone(apkTotalInfo);
		}
		catch (Exception e){
			e.printStackTrace();
			_listener.loadDone(null);
		}
//		AndroidSWInfoService.setAPKList(AndroidSWInfoService.CLASS_ID_TOP, AndroidSWInfoService.SORT_ID_HOT, 0, apkList);
		
	}
}
